<?php
class User extends Base_Controller
{
    public function __construct()
    {
        /******待编辑******/
        $this->_page_size = 20;
        $this->_form_type = 'user';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');
        $this->_controller = 'admin/user/';
        parent::__construct();
    }

    public function showList($page = 1)
    {
        $get = $this->input->get();
        if (!empty($get)){
            $where = array();
            $start = !empty($get['start'])?strtotime(trim($get['start'])):'';
            $end = !empty($get['end'])?strtotime(trim($get['end']))+24*3600:'';
            $user = !empty($get['fusername'])?trim($get['fusername']):'';
            $telnum = !empty($get['ftelnum'])?trim($get['ftelnum']):'';
            $source = !empty($get['fsource'])?trim($get['fsource']):'';
            if (!empty($start)) $where['created >'] = $start;
            if (!empty($end)) $where['created <'] = $end;
            if (!empty($user)) $where['userName LIKE']= '%'.$user.'%';
            if (!empty($telnum)) $where['telNum LIKE']= '%'.$telnum.'%';
            if (!empty($source)) $where['utype LIKE']= '%'.$source.'%';
            $this->_list_where = $where;
            $filter = $this->load->view('admin/form/filter/userlistfilter',array('controller'=>site_url($this->_controller),
                'start'=>$get['start'],'end'=>$get['end'],'fusername'=>$user,'fsource'=>$source,'ftelnum'=>$telnum),true);
        }else{
            $filter = $this->load->view('admin/form/filter/userlistfilter',array('controller'=>site_url($this->_controller)),true);
        }

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'filter'=>$filter);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    public function getPagination($uri = '')
    {
        $this->db->from('mod_user');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['status'] = (int)trim($post['status']);
            $res = $this->uploadImg('userIcon',140,140);
            if ($res['code'] == 1){
                $post['userIcon'] = $res['imgurl'][0];
            }
            $post['changed'] = time();

            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function delete($id){return false;}
}